﻿<%

Class Cls_Channel

	Dim vID
	Dim vFatherID
	Dim vChildID
	Dim vChildIDs
	Dim vDeepPath
	Dim vName
	Dim vOrder
	Dim vTable
	Dim vDomain
	Dim vOutSideLink
	Dim vTemplatechannel
	Dim vTemplateclass
	Dim vTemplateview
	Dim vRuleindex
	Dim vRulechannel
	Dim vRuleview
	Dim vPicture
	Dim vKeywords
	Dim vDescription
	Dim vModeExt
	Dim vNeedcreate
	Dim LastError
	Dim vDisplay

	Private Sub Class_Initialize()
		Call ChkLogin("channel")
		Call Initialize()
	End Sub

	Private Sub Class_Terminate()
		Call Initialize()
	End Sub

	Public Function Initialize()
		vID = 0
		vFatherID = 0
		vChildID = ""
		vDeepPath = 0
		vName = ""
		vOrder = 0
		vTable = ""
		vDomain = ""
		vOutSideLink = 0
		vTemplatechannel = "channel.html"
		vTemplateclass = "list.html"
		vTemplateview = "article.html"
		vRuleindex = "{installdir}{py}/"
		vRulechannel = "page_{page}.html"
		vRuleview = "{aid}.html"
		vPicture = ""
		vKeywords = ""
		vDescription = ""
		vNeedcreate = 1
		vModeExt=""
		vDisplay = 1

		Dim rParentID
		rParentID = Request("ParentID")
		If Len(rParentID) > 0 And IsNumeric(rParentID) And Request("sub")="yes" Then
			Dim Rs
			Set Rs = DB("Select [RuleIndex],[Domain],[Table],[Ruleview],[ModeExt] From [{pre}Channel] Where [OutSideLink]=0 And [ID]=" & rParentID,1)
			If Not Rs.Eof Then
				If Len(Rs(4) & "") >0  Then vModeExt=Rs(4)
				If Len(Rs(3) & "") > 0 Then vRuleview = Rs(3)
				If Len(Rs(2) & "") > 0 Then vTable = Rs(2)
				If Len(Rs(1) & "") > 0 Then vDomain = Rs(1) & "{py}/"
				If Len(Rs(0) & "") > 0 Then vRuleindex = Rs(0) & "{py}/"
			End If
			Rs.Close
		End If
	End Function

	Public Function GetValue()
		vFatherID = Request.Form("oFatherID")
		vDeepPath = Request.Form("oDeepPath")
		vName = Request.Form("oName")
		vOrder = Request.Form("oOrder")
		vTable = Request.Form("oTable")
		vDomain = Request.Form("oDomain")
		vOutSideLink = Request.Form("oOutSideLink")
		vTemplatechannel = Request.Form("oTemplatechannel")
		vTemplateclass = Request.Form("oTemplateclass")
		vTemplateview = Request.Form("oTemplateview")
		vRuleindex = Request.Form("oRuleindex")
		vRulechannel = Request.Form("oRulechannel")
		vRuleview = Request.Form("oRuleview")
		vPicture = Request.Form("oPicture")
		vKeywords = Request.Form("oKeywords")
		vDescription = Request.Form("oDescription")
		vModeExt = Request.Form("oModeExt")
		vDisplay = Request.Form("Display")
		if vDisplay="on" then vDisplay=0 else vDisplay=1 end if
		If Len(vOutSideLink) = 0 Or Not IsNumeric(vOutSideLink) Or vOutSideLink <> 1 Then vOutSideLink = 0

		vTemplatechannel = ReplaceX(vTemplatechannel,"{cid}","{cid}")
		vTemplateclass = ReplaceX(vTemplateclass,"{cid}","{cid}")
		vTemplateview = ReplaceX(vTemplateview,"{cid}","{cid}")

		vRuleindex = ReplaceX(vRuleindex,"{installdir}","{installdir}")
		vRuleindex = ReplaceX(vRuleindex,"{cid}","{cid}")
		vRuleindex = ReplaceX(vRuleindex,"{cen}","{cen}")
		vRulechannel = ReplaceX(vRulechannel,"{cid}","{cid}")
		vRulechannel = ReplaceX(vRulechannel,"{page}","{page}")
		vRulechannel = ReplaceX(vRulechannel,"{cen}","{cen}")
		vRuleview = ReplaceX(vRuleview,"{cid}","{cid}")
		vRuleview = ReplaceX(vRuleview,"{aid}","{aid}")
		vRuleview = ReplaceX(vRuleview,"{md5}","{md5}")
		vRuleview = ReplaceX(vRuleview,"{date}","{date}")
		vRuleview = ReplaceX(vRuleview,"{cen}","{cen}")
		vRuleview = Replace(vRuleview,"{Y}","{y}")
		vRuleview = Replace(vRuleview,"{M}","{y}")
		vRuleview = Replace(vRuleview,"{D}","{y}")
		
		vTemplatechannel = Replace(Replace(Replace(vTemplatechannel,"\","/"),"//","/"),"//","/")
		vTemplateclass = Replace(Replace(Replace(vTemplateclass,"\","/"),"//","/"),"//","/")
		vTemplateview = Replace(Replace(Replace(vTemplateview,"\","/"),"//","/"),"//","/")
		vRuleindex = Replace(Replace(Replace(Replace(vRuleindex,"\","/"),"//","/"),"//","/"),".","")
		vRulechannel = Replace(Replace(Replace(vRulechannel,"\","/"),"//","/"),"//","/")
		vRuleview = Replace(Replace(Replace(vRuleview,"\","/"),"//","/"),"//","/")
		vModeExt = Replace(vModeExt,"：",":")	
		vModeExt = Replace(vModeExt," ","")
		vModeExt = Replace(vModeExt,"'","")
		vModeExt = Replace(vModeExt,"""","")
		vModeExt = Replace(vModeExt,"<","")
		vModeExt = Replace(vModeExt,">","")
		Do While Instr(vModeExt,vbcrlf & vbcrlf)>0
			vModeExt = Replace(vModeExt,vbcrlf & vbcrlf,vbcrlf)
			
		Loop

		Dim ChannelPY
		ChannelPY = Pinyin(vName)
		If Instr(vDomain,"{py}") > 0 Then vDomain = Replace(vDomain,"{py}",ChannelPY)
		If Instr(vRuleindex,"{py}") > 0 Then vRuleindex = Replace(vRuleindex,"{py}",ChannelPY)

		If vOutSideLink = 0 Then ' 二级域名要的格式
			If LCase(Left(vDomain,7)) <> "http://" Then vDomain = ""
			If Len(vDomain) > 0 And Right(vDomain,1) <> "/" Then vDomain = vDomain & "/"
			If Seodir = 1 Then

				If Createhtml = 1 Or Createhtml = 3 Then vDomain = "" ' 目录优化不可以绑域名
			End If
		Else
			If len(vDomain) = 0 Then vDomain = "http://"
		End If

		If Right(vRuleindex,1) <> "/" Then vRuleindex = vRuleindex & "/"
		If Left(vRuleindex,1) <> "/" And Left(vRuleindex,12) <> "{installdir}" Then vRuleindex = "{installdir}" & vRuleindex
		If Left(vRulechannel,1) = "/" Then vRulechannel = Right(vRulechannel,Len(vRulechannel)-1)
		If Left(vRuleview,1) = "/" Then vRuleview = Right(vRuleview,Len(vRuleview)-1)

		If Len(vFatherID) = 0 Or Not IsNumeric(vFatherID) Then vFatherID = 0
		If Len(vDeepPath) = 0 Or Not IsNumeric(vDeepPath) Then LastError = "栏目深度只能是数字" : GetValue = False : Exit Function
		If Len(vName) < 1 Or Len(vName) > 250 Then LastError = "栏目名称的长度请控制在 1 至 250 位" : GetValue = False : Exit Function
		If Len(vOrder) = 0 Or Not IsNumeric(vOrder) Then LastError = "排列顺序只能是数字" : GetValue = False : Exit Function
		If Len(vTable) < 1 Or Len(vTable) > 20 Then LastError = "内容表的长度请控制在 1 至 20 位" : GetValue = False : Exit Function
		If Len(vDomain) > 100 Then LastError = "绑定域名的长度请控制在 100 位以内" : GetValue = False : Exit Function
		If Len(vTemplatechannel) < 1 Or Len(vTemplatechannel) > 100 Then LastError = "大类模板的长度请控制在1 至 100 位" : GetValue = False : Exit Function
		If Len(vTemplateclass) < 1 Or Len(vTemplateclass) > 100 Then LastError = "小类模板的长度请控制在 1 至 100 位" : GetValue = False : Exit Function
		If Len(vTemplateview) < 1 Or Len(vTemplateview) > 100 Then LastError = "预览页模板的长度请控制在 1 至 100 位" : GetValue = False : Exit Function
		If Len(vRuleindex) < 1 Or Len(vRuleindex) > 100 Then LastError = "栏目存放目录的长度请控制在 1 至 100 位" : GetValue = False : Exit Function
		If Len(vRulechannel) < 1 Or Len(vRulechannel) > 100 Then LastError = "栏目分页规则的长度请控制在 1 至 100 位" : GetValue = False : Exit Function
		If Len(vRuleview) < 1 Or Len(vRuleview) > 100 Then LastError = "预览页规则的长度请控制在 1 至 100 位" : GetValue = False : Exit Function
		If Len(vPicture) > 100 Then LastError = "形象图的长度请控制在100 位以内" : GetValue = False : Exit Function
		If Len(vKeywords) > 100 Then LastError = "栏目关键字的长度请控制在 100 位以内" : GetValue = False : Exit Function
		If Len(vDescription) > 250 Then LastError = "栏目描述的长度请控制在 250 位以内" : GetValue = False : Exit Function
		vKeywords = GetDescription(vKeywords)
		vDescription = GetDescription(vDescription)
		GetValue = True
	End Function

	Public Function SetValue()
		Dim Rs
		Set Rs = DB("Select * From [{pre}Channel] Where [ID]=" & vID,1)
		If Rs.Eof Then Rs.Close : Set Rs = Nothing : LastError = "你所需要查询的记录 " & vID & " 不存在!" : SetValue = False : Exit Function
		vFatherID = Rs("FatherID")
		vChildID = Rs("ChildID")
		vChildIDs = Rs("ChildIDs")
		vDeepPath = Rs("DeepPath")
		vName = Rs("Name")
		vOrder = Rs("Order")
		vTable = Rs("Table")
		vDomain = Rs("Domain")
		vOutSideLink = Rs("OutSideLink")
		vTemplatechannel = Rs("Templatechannel")
		vTemplateclass = Rs("Templateclass")
		vTemplateview = Rs("Templateview")
		vRuleindex = Rs("Ruleindex")
		vRulechannel = Rs("Rulechannel")
		vRuleview = Rs("Ruleview")
		vPicture = Rs("Picture")
		vKeywords = Rs("Keywords")
		vDescription = Rs("Description")
		vNeedcreate = Rs("Needcreate")
		vModeExt = Rs("ModeExt")
		vDisplay = Rs("Display")
		Rs.Close : Set Rs = Nothing
		SetValue = True
	End Function

	Public Function Create()
		Dim Rs
		If Len(vDomain) > 0 Then
			Set Rs = DB("Select [ID] From [{pre}Channel] Where [Domain]='" & vDomain & "'",1)
			If Not Rs.Eof Then Rs.Close : Set Rs = Nothing : LastError = "绑定域名 的值 " & vDomain & " 已存在!" : Create = False : Exit Function
		End If
		' 计算深度
		If vFatherID > 0 Then
			vDeepPath = DB("Select [DeepPath] From [{pre}Channel] Where [ID]=" & vFatherID,1)(0) + 1
		Else
			vDeepPath = 0
		End If
		Set Rs = DB("Select * From [{pre}Channel]",3)
		Rs.AddNew
		Rs("ChildID") = ""
		Rs("ChildIDs") = ""
		Rs("FatherID") = vFatherID
		Rs("DeepPath") = vDeepPath
		Rs("Name") = vName
		Rs("Order") = vOrder
		Rs("Table") = vTable
		Rs("Domain") = vDomain
		Rs("OutSideLink") = vOutSideLink
		Rs("Templatechannel") = vTemplatechannel
		Rs("Templateclass") = vTemplateclass
		Rs("Templateview") = vTemplateview
		Rs("Ruleindex") = vRuleindex
		Rs("Rulechannel") = vRulechannel
		Rs("Ruleview") = vRuleview
		Rs("Picture") = vPicture
		Rs("Keywords") = vKeywords
		Rs("Description") = vDescription
		Rs("Needcreate") = 1
		Rs("ModeExt") = vModeExt
		Rs("Display") = vDisplay
		Rs.Update : Rs.Close : Set Rs = Nothing
		Call DB("Update [{pre}channel] Set [Cid]=[ID]",0)
		Call setTable(vTable) ' 创建表
		Call ReloadChildID() ' 更新子类
		call createchannelcache() ' 更新栏目缓存
		Call ClsCache() ' 清空下缓存
		If Createhtml = 1 And vFatherID  > 0 Then Call CreateChannel(vFatherID,1) ' 更新上级栏目首页
		'If Createhtml = 1 Then Call CreateChannel(DB("Select [ID] From [{pre}Channel] Order By [ID] Desc",1)(0),1) ' 生成栏目首页
		If Createhtml = 1 Then Call CreateIndex(1) ' 生成首页
		Create = True
	End Function

	Public Function Modify()
		Dim Rs,ChildDeepPath,Childs,i
		If Len(vDomain) > 0 Then
			Set Rs = DB("Select [ID] From [{pre}Channel] Where [ID]<>" & vID & " And [Domain]='" & vDomain & "'",1)
			If Not Rs.Eof Then Rs.Close : Set Rs = Nothing : LastError = "绑定域名 的值 " & vDomain & " 已存在!" : Modify = False : Exit Function
		End If
		Set Rs = DB("Select * From [{pre}Channel] Where [ID]=" & vID,3)
		If Rs.Eof Then Rs.Close : Set Rs = Nothing : LastError = "你所需要更新的记录 " & vID & " 不存在!" : Modify = False : Exit Function
		' 重新计算深度
		If vFatherID > 0 Then
			vDeepPath = DB("Select [DeepPath] From [{pre}Channel] Where [ID]=" & vFatherID,1)(0) + 1
		Else
			vDeepPath = 0
		End If

		If Len(Rs("ChildIDs"))-Len(vID & ",") > 0 Then
			Childs = Right(Rs("ChildIDs"),Len(Rs("ChildIDs"))-Len(vID & ","))
			If Len(Childs) > 0 Then
				ChildDeepPath = Rs("DeepPath") - vDeepPath
				Call DB("Update [{pre}Channel] Set [DeepPath]=[DeepPath]-" & ChildDeepPath & " Where [ID] In (" & Rs("ChildIDs") & ")",0)
			End If
		End If

		If Rs("Domain") <> vDomain Or  Rs("Ruleindex") <> vRuleindex Or Rs("Rulechannel") <> vRulechannel Or Rs("Ruleview") <> vRuleview Then
			Call DB("Update [{pre}Content] Set [Filepath]='',[Viewpath]='' Where [Cid]=" & vID,0)
		End If
		
		Rs("DeepPath") = vDeepPath
		Rs("FatherID") = vFatherID
		Rs("Name") = vName
		Rs("Order") = vOrder
		Rs("Domain") = vDomain
		Rs("OutSideLink") = vOutSideLink
		Rs("Templatechannel") = vTemplatechannel
		Rs("Templateclass") = vTemplateclass
		Rs("Templateview") = vTemplateview
		Rs("Ruleindex") = vRuleindex
		Rs("Rulechannel") = vRulechannel
		Rs("Ruleview") = vRuleview
		Rs("Picture") = vPicture
		Rs("Keywords") = vKeywords
		Rs("Description") = vDescription
		Rs("ModeExt") = vModeExt
		Rs("Display") = vDisplay
		Rs.Update : Rs.Close : Set Rs = Nothing
		Call DB("Update [{pre}channel] Set [Cid]=[ID]",0)
		Call setTable(vTable) ' 创建表
		Call ReloadChildID() ' 更新子类
		call createchannelcache() ' 更新栏目缓存
		Call ClsCache() ' 清空下缓存
		Modify = True
	End Function

	Public Function Delete()
		Call SetValue()
		Dim Rs,i,IDs
		IDs = Split(vChildIDs,",")
		For i = 0 To Ubound(IDs)
			If Len(IDs(i)) > 0 And IsNumeric(IDs(i)) Then
			Set Rs = DB("Select [Table],[Ruleindex] From [{pre}Channel] Where [ID]=" & IDs(i),1)
			If Not Rs.Eof Then
				Call DeleteFolder(Replace(Replace(Rs(1),"{installdir}",Installdir),"{cid}",IDs(i))) '删除目录
				if chkTable(Rs(0)) then
					Call DB("Delete From [" & Rs(0)  & "] Where [Cid]=" & IDs(i),0) ' 删除文章内容
					If DB("Select Count([ID]) From [{pre}Channel] Where [Table]='" & Rs(0) & "'",1)(0) = 1 Then Call DB("Drop Table [" & Rs(0) & "]",0) ' 删除内容数据表
				end if
			End If
			Rs.Close : Set Rs = Nothing
			End If
		Next
		Call DB("Delete From [{pre}Content] Where [Cid] In(" & vChildIDs & ")",0)       ' 删除文章(不删除生成的文件)
		Call DB("Update [{pre}Upload] Set [Aid]=0 Where [Cid] In(" & vChildIDs & ")",0) ' 删除上传文件(只打个记号)
		Call DB("Delete From [{pre}Channel] Where [ID] In(" & vChildIDs & ")",0)        ' 删除栏目信息
		if instr("/" & getplus,"/comment") > 0 then Call DB("Delete From [{pre}Comment] Where [Cid] In(" & vChildIDs & ")",0)       ' 删除评论
		Call ReloadChildID() ' 了类
		call createchannelcache() ' 更新栏目缓存
		Call ClsCache() ' 清空下缓存
		Delete = True
	End Function
	
	Function ReloadChildID()
		Dim Rs,Tmp,MaxDeepPath
		Set Rs = DB("Select Top 1 [DeepPath] From [{pre}Channel] Order By [DeepPath] Desc",1)
		If Not Rs.Eof Then MaxDeepPath = Rs(0) Else MaxDeepPath = 0
		Rs.Close
		Set Rs = DB("Select [ID],[ChildID],[ChildIDs],[DeepPath] From [{pre}Channel]",3)
		Do While Not Rs.Eof
			If Rs(3) = MaxDeepPath Then ' 最后一层没有子栏目
				Rs("ChildID") = ""
				Rs("ChildIDs") = Rs(0)
			Else
				Tmp = GetChildID(Rs(0),0) ' 找所有一级子栏目
				If MaxDeepPath - 1 = Rs(3) Then ' 最后第二层的一级子栏目和所有子栏目相同滴
					Rs("ChildID") = Tmp
					If Len(Tmp) = 0 Then Tmp = Rs(0) Else Tmp = Rs(0) & "," & Tmp
					Rs("ChildIDs") = Tmp
				Else
					Rs("ChildID") = Tmp
					If Len(Tmp) = 0 Then
						Tmp = Rs(0) 
					Else
						Tmp = Rs(0) & "," & GetChildID(Rs(0),1)
					End If
					Rs("ChildIDs") = Tmp
				End If
			End If
			Rs.Update
			Rs.MoveNext
		Loop
		Rs.Close : Set Rs = Nothing
	End Function
	
	Function GetChildID(Byval ID,Byval Deep)
		Dim Rs
		Set Rs = DB("Select [ID] From [{pre}Channel] Where [FatherID]=" & ID & " Order By [Order] Desc",1)
		Do While Not Rs.Eof
			If Deep = 0 Then
				GetChildID = GetChildID & "," & Rs(0) ' 仅下级栏目
			Else
				GetChildID = GetChildID & "," & Rs(0) & "," & GetChildID(Rs(0),1) ' 所有子栏目
			End If
			Rs.MoveNext
		Loop
		Rs.Close : Set Rs = Nothing
		GetChildID = Replace(GetChildID,",,",",") : GetChildID = Replace(GetChildID,",,",",")
		If Right(GetChildID,1) = "," Then GetChildID = Left(GetChildID,Len(GetChildID)-1)
		If Left(GetChildID,1) = "," Then GetChildID = Right(GetChildID,Len(GetChildID)-1)
	End Function


End Class
%>